package com.aop.zy.aop.aspect;

import com.aop.zy.aop.annotation.PrintTime;
import com.aop.zy.aop.controller.AopController;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.aspectj.lang.reflect.SourceLocation;
import org.springframework.stereotype.Component;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author zhangyi
 * @date 2018/12/23 20:40
 */
@Aspect
@Component
public class PrintTimeAsp {
//
//    @Pointcut("@annotation(com.aop.zy.aop.annotation.PrintTime)")
//    public void printTimePoint(){}
//
//    @Around("printTimePoint()")
//    public void printTime(JoinPoint joinPoint){
//        Date date = new Date(System.currentTimeMillis());
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
//        String nowDate = simpleDateFormat.format(date);
//        System.out.println("切面打印"+nowDate);
//        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
//        Method method = signature.getMethod();
//        PrintTime annotation = method.getAnnotation(PrintTime.class);
//        if(!Objects.isNull(annotation)){
//            System.out.println("已经加上时间注解操作");
//        }
//        AnnotatedType annotatedReturnType = method.getAnnotatedReturnType();
//        Type type = annotatedReturnType.getType();
//        String typeName = type.getTypeName();
//        System.out.println("返回值类型: "+typeName);
//    }

//    @Pointcut("@annotation(com.aop.zy.aop.annotation.AroundTime)")
//    public void aroundTime(){}
//
//    @Pointcut("execution(* com.aop.zy.aop.controller..*.*(..))")
//    public void aroundTime(){}
//
//
//    @Around("aroundTime()")
//    public Object aroundT(ProceedingJoinPoint joinPoint){
//        Object proceed = null;
//        try {
//            System.out.println("开始记录");
//             proceed = joinPoint.proceed();
//            System.out.println(proceed+"----------");
//            System.out.println("结束记录");
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        return proceed;
//    }

    @Pointcut("execution(* com.aop.zy.aop.controller..*.*(..))")
    public void allController(){}

    @Before("allController()")
    public void allC(JoinPoint joinPoint){

        String kind = joinPoint.getKind();
        System.out.println(kind);


        Object target = joinPoint.getTarget();
        if(target instanceof AopController){
            AopController aopController = (AopController)target;
            aopController.cyc();
        }

        Object aThis = joinPoint.getThis();
        System.out.println("当前控制类"+aThis);


        Object[] args = joinPoint.getArgs();

        for(Object o : args){
            System.out.println("到参数: "+o);
        }
    }

}
